{% load i18n %}
<script>
// set global variables
var ediUrl = '/mdtools/rndt/{{ layername }}/';
queryStringValues = {{ queryStringValues }} 
</script>
<script type="text/javascript" src="{{ STATIC_URL }}edi/js/1.00/xml2json.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}edi/js/1.00/json2xml.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}edi/js/1.00/langs.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}edi/js/1.00/spin.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}geosk/js/rndtCallbacks.js"></script>
<script type="text/javascript" src="{{ STATIC_URL }}edi/js/2.00/mdeditor.js"></script>

<script type="text/javascript">
var edimlUrl = "/proxy/?url=http%3A%2F%2Fsp7.irea.cnr.it%2Fjboss%2FMDService%2Frest%2Fediml%2F";

function checkRequired(){
    var messages = [];
    var errors = false;
    var required = $(['ling_md_1', 'resp_md_1', 'resp_md_2', 'tit_1', 'data_1', 'data_2'])
    $(required).each(
	function(i, val){
	    var el = $("#" + val);
	    if(!el.val()){
		errors = true;
		el.closest('.control-group').addClass('error');
		el.attr("placeholder", "This field is required");
	    }
	}
    );   

    return errors;
}

function checkMandatory(){
    var i=0;
    var n=0;
    var warnings = false;
    $("[required='required'][type!='hidden']").each(
	function(id, el){
	    if($(el).css('display') != 'none'){
		if( ($(el).val() && $(el).val()) || $(el).attr('type')=='checkbox' ){ //skip checkbox 
		    i++ 
		} else {
		    console.log('REQUIRED');
		    console.log(el);
		    warnings = true;
		}
		n++
	    }
	}
    );
    return warnings
}

function beforeSend(jqXHR, settings){
    // show gif here, eg:
    if(settings.url == 'postMetadata'){
	$("#loading-modal").modal({
	    keyboard: false,
	    backdrop: 'static'
	});
        var errors = checkMandatory();
        if(errors){
	    closeModal();
	    $("#error-message").text("{% trans 'Some of the Mandatory fields are not filled.  Please verify your data and try again.' %}")
	    $("#loading-alert").modal('show');
	    jqXHR.abort();
        }

    }
}
function complete(jqXHR, settings){
    // hide gif here, eg:
    if(settings.url == 'postMetadata'){
	$("#loading-modal").modal('hide');
    }
}

$.ajaxSetup({
    beforeSend: beforeSend,
    complete: complete
});

// side bar
$('body').scrollspy(); //function(){offset:{top: 10}}
setTimeout(function () {
    $('.bs-docs-sidenav').affix({
        offset: {
	    top: 10,
	    bottom: 270
        }
	});
}, 100)

{% get_current_language as  LANGUAGE_CODE %}

$(document).ready(function() {
    $('.mandatory').addClass('text-info');
    setLanguage('{{LANGUAGE_CODE}}');
    // var required = $(['ling_md_1', 'resp_md_1', 'resp_md_2', 'tit_1', 'data_1', 'data_2'])
    // $(required).each(
    // 	function(i, val){
    // 	    var el = $("#" + val);
    // 	    el.closest('.control-group').find('label').removeClass('text-info').addClass('text-error');
    // 	}
    // );   

});
</script>
    
<style>
#mdcontent .uris {
    display: none;
}
</style>
